PHP search function

use PHP and MYSQL to build search function

Full Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
$keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';
$con= new mysqli("localhost","root","","search");
if (mysqli_connect_errno($con))
{
echo "Error: " . mysqli_connect_error();
}
if (!mysqli_set_charset($con, "utf8")) {
printf("Error loading character set utf8: %s\n", mysqli_error($con));
} else {
printf("START SEARCHING");
}
$rs= mysqli_query($con,"SELECT * FROM user WHERE username LIKE '%{$keywords}%'");
$users = array();//save users
if(!empty($keywords)){
while ($row=mysqli_fetch_assoc($rs)){
$row['username'] = str_replace($keywords,'<font color="red">'.$keywords.'</font>',$row['username']);
$users[] = $row;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>search function</title>
<style>
.textbox {
width: 355px;
height: 40px;
border-radius: 3px;
border: 1px solid #e2b709;
padding-left: 10px;
}
.su {
width: 365px;
height: 40px;
background-color: #7fbdf0;
color: white;
border: 1px solid #666666;
}
table{ background-color: #7fbdf0; line-height:25px;}
th{ background-color:#fff;}
td{ background-color:#fff; text-align:center}
</style>
</head>
<body >
<form action="" method="get">
<p><input type="text" name="keywords" value="" placeholder="input"/>
<p><input type="submit" value="search"/>
</form>
<?php
if ($keywords){
echo '<h3>keywords:<font color="red">'.$keywords.'</font></h3>';
}
if ($users){
echo '<table width="500" cellpadding="5" >';
echo '<tr><th>username</th><th>password</th><th>email</th><th>sex</th><th>hobby</th>';
foreach ($users as $key=>$value){
echo '<tr>';
echo '<td>'.$value['username'].'</td>';
echo '<td>'.$value['password'].'</td>';
echo '<td>'.$value['sex'].'</td>';
echo '<td>'.$value['email'].'</td>';
echo '<td>'.$value['hobby'].'</td>';
echo '</tr>';
}
}else{
echo 'None';
}
?>
</body>
</html>

“Fatal error: Uncaught Error: Call to undefined function mysql_connect()”solve

1
2
3
4
5
6
7
8
// mysqli
$mysqli = new mysqli("example.com", "user", "password", "database");
// PDO
$pdo = new PDO('mysql:host=example.com;dbname=database', 'user', 'password');
// mysql
$c = mysql_connect("example.com", "user", "password");

php.ini:

1
2
3
4
5
6
7
8
extension=php_curl.dll
extension=php_gd2.dll
extension=php_mbstring.dll
;extension=php_mysql.dll //deleted by php7
extension=php_mysqli.dll
extension=php_pdo_mysql.dll
extension=php_pdo_odbc.dll
extension=php_xmlrpc.dll

hello.php changes:

1
2
3
4
5
6
7
$dbc= new mysqli("localhost","root","root","test");
if(!$dbc) {
echo"error!";
}else{
echo"success";
}
mysqli_close($dbc);

repostlinks